<template>
  <xh-container btnText="保存" @onSave="handleClickSubmit" title="编辑">
    <view class="container">
      <uni-forms
        ref="form1"
        :rules="rules1"
        :modelValue="formData"
        :label-width="80"
        label-align="right"
      >
        <uni-forms-item label="检验日期" name="inspectDate" required>
          <uni-datetime-picker
            type="date"
            v-model="formData.inspectDate"
            :start="Date.now()"
          />
        </uni-forms-item>
        <uni-forms-item label="检验类型" name="type" required>
          <uni-data-select
            v-model="formData.type"
            :localdata="typeOptions"
            @change="handleChangeType"
            disabled
          ></uni-data-select>
        </uni-forms-item>
        <uni-forms-item label="产品名称" name="productName" required>
          <xh-data-select
            v-model="formData.productName"
            :localdata="productList"
            @change="handleChangeProduct"
          ></xh-data-select>
        </uni-forms-item>
        <uni-forms-item
          label="柜号"
          name="cabinetNum"
          v-if="formData.type === '0'"
        >
          <xh-data-select
            v-model="formData.cabinetNum"
            :localdata="cabinetNumList"
          ></xh-data-select>
        </uni-forms-item>
        <uni-forms-item
          v-if="formData.type === '1' && isHasVal(formData.insCode)"
          label="检验编码"
          name="insCode"
        >
          <xh-data-select v-model="formData.insCode" :localdata="inspectList" />
        </uni-forms-item>
        <uni-forms-item
          label="裁片编码"
          name="cuttingCode"
          v-if="formData.type === '0' && isHasVal(formData.cuttingCode)"
        >
          <xh-data-select
            v-model="formData.cuttingCode"
            :localdata="cuttingcodeList"
          />
        </uni-forms-item>
        <template v-if="cRole3 || formData.type === '0'">
          <uni-forms-item label="数量" name="quantity">
            <uni-easyinput
              type="digit"
              v-model="formData.quantity"
              placeholder="数量"
            />
          </uni-forms-item>
          <uni-forms-item label="结论" name="conclusion">
            <uni-data-select
              v-model="formData.conclusion"
              :localdata="conclusionList"
            />
          </uni-forms-item>
        </template>
        <template v-if="formData.type === '1'">
          <template v-if="cRole1">
            <uni-forms-item label="克重" name="gsm">
              <uni-easyinput
                type="digit"
                v-model="formData.gsm"
                placeholder="请输入克重"
              />
            </uni-forms-item>
            <uni-forms-item label="门幅" name="fabricWidth">
              <uni-easyinput
                type="digit"
                v-model="formData.fabricWidth"
                placeholder="请输入门幅"
              />
            </uni-forms-item>
          </template>
          <template v-if="cRole2">
            <uni-forms-item label="读色数据" name="readColorData">
              <uni-easyinput
                type="digit"
                v-model="formData.readColorData"
                placeholder="请输入读色数据"
              />
            </uni-forms-item>
            <uni-forms-item label="读色结果" name="readColorResult">
              <uni-data-select
                v-model="formData.readColorResult"
                :localdata="readColorResultList"
              />
            </uni-forms-item>
          </template>
        </template>
      </uni-forms>
    </view>
  </xh-container>
</template>

<script>
import { decodeParams } from "@/utils/common.js";
import { BottleTypeEnum } from "@/utils/constant";
import { formatSelectList } from "@/utils/business.js";

import { getRecordBillProduct, getInsCodeList } from "@/api/purchase/fabric.js";
import {
  getBomProductList,
  getCabinetList,
  getCuttingcodeList,
  editBottle,
} from "@/api/purchase/bottleInspect.js";
import {
  getFabricInspectionConclusionDict,
  getBottleInspectionConclusionDict,
} from "@/api/system/dict.js";
import AppStorage from "@/utils/appStorage";
import { Role } from "@/utils/constant";
export default {
  data() {
    return {
      roleKeys: [],
      formData: {},
      rules1: {},
      typeOptions: BottleTypeEnum,
      productList: [],
      inspectList: [],
      cabinetNumList: [],
      conclusionList: [], // 结论
      cuttingcodeList: [], // 裁片编码
    };
  },
  onLoad(options) {
    this.formData = decodeParams(options);
  },
  created() {},
  computed: {
    cRole1() {
      return (
        this.roleKeys.includes(Role.fabricInspector1) ||
        this.roleKeys.includes(Role.admin)
      );
    },
    cRole2() {
      return (
        this.roleKeys.includes(Role.fabricInspector2) ||
        this.roleKeys.includes(Role.admin)
      );
    },
    cRole3() {
      return (
        this.roleKeys.includes(Role.fabricBottleInspector) ||
        this.roleKeys.includes(Role.admin) ||
        this.formData.type === "0"
      );
    },
  },
  mounted() {
    const { type } = this.formData;
    this.fetchProductList();
    if (type === "1") {
      this.fetchInsCodeList();
      this.fetchConclusionDict1();
    } else {
      this.fetchConclusionDict0();
      this.handleChangeCabinet();
      this.fetchCabinetList();
    }
    const userInfo = AppStorage.get("userInfo");
    this.roleKeys = userInfo.roleKeys;
  },

  methods: {
    isHasVal(val) {
      return !(val === null || val === undefined);
    },
    handleChangeProduct(item) {
      const { cabinetNum } = item;
      this.formData.cabinetNum = cabinetNum;
      if (this.formData.type === "0") {
        this.fetchCabinetList();
      }
    },
    handleChangeCabinet() {
      const { productName, cabinetNum, cuttingCode } = this.formData;
      getCuttingcodeList({
        productName,
        cabinetNum,
        cuttingCode,
      }).then((res) => {
        const { rows } = res;
        this.cuttingcodeList = formatSelectList(rows);
      });
    },
    // 请求检验编码数据
    fetchInsCodeList() {
      const { productName, insCode } = this.formData;
      if (productName) {
        getInsCodeList(productName, "0", insCode).then((res) => {
          const { rows } = res;
          this.inspectList = formatSelectList(rows, "insCode");
        });
      }
    },
    // 请求柜号数据
    fetchCabinetList() {
      const { productName } = this.formData;
      getCabinetList({
        productName,
      }).then((res) => {
        const { rows } = res;
        this.cabinetNumList = formatSelectList(rows);
      });
    },
    handleClickSubmit() {
      editBottle(this.formData).then(() => {
        this.$modal.msgSuccess("编辑成功！");
        this.$nav.navigateBack();
      });
    },
    fetchProductList() {
      const { type } = this.formData;
      if (type === "0") {
        getBomProductList().then((res) => {
          const { rows } = res;
          this.productList = formatSelectList(rows, "productName");
        });
      } else {
        getRecordBillProduct({
          bottlesResult: "0",
          category: "001",
        }).then((res) => {
          const { rows } = res;
          this.productList = formatSelectList(rows, "productName");
        });
      }
    },
    fetchConclusionDict1() {
      getFabricInspectionConclusionDict().then((res) => {
        const { data } = res;
        this.conclusionList = formatSelectList(data, {
          text: "dictLabel",
          value: "dictValue",
        });
      });
    },
    fetchConclusionDict0() {
      getBottleInspectionConclusionDict().then((res) => {
        const { data } = res;
        this.conclusionList = formatSelectList(data, {
          text: "dictLabel",
          value: "dictValue",
        });
      });
    },
  },
};
</script>
<style scoped lang="scss">
.container {
  padding: 10px;
}
</style>
